﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IDML.BusinessObject;
using IDML.BusinessLogic;


namespace IDML.Web.Modules.Patient
{
    public partial class TestResult : System.Web.UI.Page
    {
        int patientId = 0;
        int testId = 0;
        #region "Events"

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Request.QueryString["PatientId"].ToString() != null)
                //{
                //    patientId = int.Parse(Request.QueryString["PatientId"]);
                //    CreateFormControls();
                //}                
                    patientId = 1;
                    ViewState.Add("PatientId",patientId);
                    ViewState.Add("TestId", testId);
                    CreateFormControls();               
            }
            else
            {
                CreateFormControls();   
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            TestStructureBO[] newArray = (TestStructureBO[])ViewState["TestResultTextBO"];
            List<TestStructureBO> newList = new List<TestStructureBO>(newArray);
            TestBL reportBL = new TestBL();
            foreach (TestStructureBO item in newList)
            {
                string myval = null;
                if (Panel1.FindControl("txt" + item.TestStructureId) != null)
                {
                    TextBox tb = (TextBox)Panel1.FindControl("txt" + item.TestStructureId);
                    myval = tb.Text;
                }
                else if (Panel1.FindControl("ddl" + item.TestStructureId) != null)
                {
                    DropDownList ddl = (DropDownList)Panel1.FindControl("ddl" + item.TestStructureId);
                    myval = ddl.SelectedValue;                
                }

                PatientTestResultBO ptr=new PatientTestResultBO();
                ptr.PatientId = int.Parse(ViewState["PatientId"].ToString());
                ptr.TestStructureId = item.TestStructureId;
                ptr.Value = myval;
                int success = reportBL.PatientTestResult_Insert(ptr);
            }
        }
        #endregion

        #region "Methods and Functions"
        private void CreateFormControls()
        {
            TestBL reportBL = new TestBL();
            TestBO testBO = new TestBO();
            testBO.TestId = 1;
            List<TestStructureBO> rtList = reportBL.GetTestStructure_Listst(testBO);
            ValidationBO v = new ValidationBO();
            v.TestId = 1;
            List<ValidationBO> vlList = reportBL.GetValidationData(v);

            ViewState.Add("TestResultTextBO", rtList.ToArray());
            foreach (TestStructureBO item in rtList)
            {
               //===Text Lable Generate

                Label lable = new Label();               
                lable.ID = "lbl" + item.TestStructureId;
                lable.Text = item.TestStructureText;
                Panel1.Controls.Add(lable);

                if (item.ControlId==1)//TextBox
                {
                TextBox textBox = new TextBox();
                textBox.ID = "txt" + item.TestStructureId;           
                Panel1.Controls.Add(textBox);

                List<ValidationBO> vlList1 = new List<ValidationBO>();
                vlList1 = vlList.FindAll(x => x.TestStructureId == item.TestStructureId);
                //v = vlList.Find(x => x.TestStructureId == item.TestStructureId);
                if (v != null)
                {

                    foreach (ValidationBO vl in vlList1)
                    {
                        if (vl.ValidationTypeId == 1)//Required Field Validator
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            rfv.ControlToValidate = textBox.ID;
                            rfv.ErrorMessage = vl.Message;
                            Panel1.Controls.Add(rfv);

                        }
                        else if (vl.ValidationTypeId == 2) //Regular Expressions
                        {
                            RegularExpressionValidator rev = new RegularExpressionValidator();
                            rev.ControlToValidate = textBox.ID;
                            rev.ErrorMessage = vl.Message;
                            rev.ValidationExpression = vl.Expression;
                            Panel1.Controls.Add(rev);
                        }
                    }

                }

                }
                else if (item.ControlId == 2)//DropDownList
                {
                    DropDownList dropDownList = new DropDownList();
                    dropDownList.ID = "ddl" + item.TestStructureId;
                    MasterListDataBO masterListDataBO = new MasterListDataBO();
                    masterListDataBO.TestStructureId = item.TestStructureId;
                    dropDownList.DataSource = reportBL.GetMasterListData(masterListDataBO);
                    dropDownList.DataTextField = "Value"; 
                    dropDownList.DataValueField = "MasterListDataId";
                    dropDownList.DataBind();                  

                    Panel1.Controls.Add(dropDownList);
                }
                else if (item.ControlId == 3)//ComputationalField                          
                               
                {

                    Button b = new Button();
                    b.ID = "btn" + item.TestStructureId ; // 
                    b.Text = "Calculate";
                    b.ToolTip = "Calculate";
                    b.CommandArgument = item.TestStructureId.ToString(); //passing this to event handler
                    b.Command += new CommandEventHandler(b_Command); //handler
                    Panel1.Controls.Add(b);

                    Label lable2 = new Label();
                    lable2.ID="lbl1"+ item.TestStructureId ;
                    lable2.Enabled = false;
                   
                    Panel1.Controls.Add(lable2);
                }


                LiteralControl lc = new LiteralControl("<br />");
                Panel1.Controls.Add(lc);

                
            }

      



        }

        void b_Command(object sender, CommandEventArgs e)
        {
           // e.CommandArgument.ToString()

            TestStructureBO[] newArray1 = (TestStructureBO[])ViewState["TestResultTextBO"];
            List<TestStructureBO> newList1 = new List<TestStructureBO>(newArray1);

            var query1 = newList1.Where(item => item.DependTestStructureId == int.Parse(e.CommandArgument.ToString()));

            double cal = 0;
            foreach (TestStructureBO q in query1)
            {
                if (q.Operator != "")
                {
                    if (Panel1.FindControl("txt" + q.TestStructureId) != null)
                    {
                        TextBox tb = (TextBox)Panel1.FindControl("txt" + q.TestStructureId);
                        cal = Calculation(cal, double.Parse(tb.Text), q.Operator.Trim());
                    }
                }
                else
                {
                    if (Panel1.FindControl("txt" + q.TestStructureId) != null)
                    {
                        TextBox tb = (TextBox)Panel1.FindControl("txt" + q.TestStructureId);
                        cal = double.Parse(tb.Text);
                    }               
                
                }
            }

            Label lb = (Label)Panel1.FindControl("lbl1" + int.Parse(e.CommandArgument.ToString()));
            lb.Text = cal.ToString();


        }


        private double Calculation(double i1, double i2, string Op)
        {
            double final = 0;
            if (Op == "+")
            {
                final = i1 + i2;
            }
            else if (Op == "-")
            {
                final = i1 - i2;
            }
            else if (Op == "/")
            {
                final = i1 / i2;
            }
            else if (Op == "*")
            {
                final = i1 * i2;
            }
            else
            {
                final = 0;
            }

            return final;

        }
        
        
        #endregion



    }
}